package com.party.web.web.controller.sign;

import com.party.common.constant.Constant;
import com.party.common.paging.Page;
import com.party.common.utils.StringUtils;
import com.party.core.model.sign.*;
import com.party.core.service.sign.ISignApplyService;
import com.party.core.service.sign.ISignGroupService;
import com.party.core.service.sign.ISignProjectService;
import com.party.core.service.sign.ISignRecordService;
import com.party.web.biz.sign.SignApplyBizService;
import com.party.web.web.annotation.ControlParam;
import com.party.web.web.annotation.DataControl;
import com.party.web.web.dto.AjaxResult;
import com.party.web.web.dto.input.common.CommonInput;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

/**
 * 签到小组成员控制器
 * Created by wei.li
 *
 * @date 2017/6/6 0006
 * @time 14:02
 */
@Controller
@RequestMapping(value = "sign/member")
public class SignMemberController {

    @Autowired
    private ISignApplyService signApplyService;

    @Autowired
    private ISignGroupService signGroupService;

    @Autowired
    private SignApplyBizService signApplyBizService;

    @Autowired
    private ISignProjectService signProjectService;

    @Autowired
    private ISignRecordService signRecordService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 签到小组成员列表
     * @param groupMember 签到小组成员
     * @param page 分页参数
     * @return 成员列表
     */
    @DataControl
    @RequestMapping(value = "list")
    public ModelAndView list(@ControlParam(table = "sign_project", column = "create_by", getMethodName = "getProjectId")GroupMember groupMember, String newGroupId, Page page, CommonInput commonInput){
        page.setLimit(20);
        if (StringUtils.isNotEmpty(newGroupId)) {
            groupMember.setGroupId(newGroupId);
        }
        ModelAndView modelAndView = new ModelAndView("sign/signApplyList");
        Map<String, Object> params = CommonInput.appendParams(commonInput);
        groupMember.setStatus(SignApplyStatus.PASS_STATUS.getCode());
        List<GroupMember> list = signApplyService.memberList(groupMember, params, page);
        SignProject signProject = signProjectService.get(groupMember.getProjectId());
        if (StringUtils.isNotEmpty(newGroupId)) {
            SignGroup myGroup = signGroupService.get(groupMember.getGroupId());
            modelAndView.addObject("myGroup", myGroup);
        }
        //所属队伍
        SignGroup signGroup = new SignGroup();
        signGroup.setProjectId(groupMember.getProjectId());
        List<SignGroup> signGroupList = signGroupService.list(signGroup);
        modelAndView.addObject("signGroupList", signGroupList);
        modelAndView.addObject("list", list);
        modelAndView.addObject("page", page);
        modelAndView.addObject("signProject", signProject);
        modelAndView.addObject("input", commonInput);
        return modelAndView;
    }

    /**
     * 签到小组成员审核列表
     * @param groupMember 签到小组成员
     * @param page 分页参数
     * @return 成员列表
     */
    @DataControl
    @RequestMapping(value = "check/list")
    public ModelAndView checkList(@ControlParam(table = "sign_project", column = "create_by", getMethodName = "getProjectId")GroupMember groupMember, String newGroupId, Page page, CommonInput commonInput){
        page.setLimit(20);
        if (StringUtils.isNotEmpty(newGroupId)) {
            groupMember.setGroupId(newGroupId);
        }
        ModelAndView modelAndView = new ModelAndView("sign/signApplyCheckList");
        Map<String, Object> params = CommonInput.appendParams(commonInput);
        groupMember.setStatus(SignApplyStatus.NOT_AUDIT_STATUS.getCode());
        List<GroupMember> list = signApplyService.memberList(groupMember, params, page);
        SignProject signProject = signProjectService.get(groupMember.getProjectId());
        if (StringUtils.isNotEmpty(newGroupId)) {
            SignGroup myGroup = signGroupService.get(groupMember.getGroupId());
            modelAndView.addObject("myGroup", myGroup);
        }
        //所属队伍
        SignGroup signGroup = new SignGroup();
        signGroup.setProjectId(groupMember.getProjectId());
        List<SignGroup> signGroupList = signGroupService.list(signGroup);
        modelAndView.addObject("signGroupList", signGroupList);
        modelAndView.addObject("list", list);
        modelAndView.addObject("page", page);
        modelAndView.addObject("signProject", signProject);
        modelAndView.addObject("input", commonInput);
        return modelAndView;
    }

    /**
     * 签到小组成员列表
     * @param groupMember 签到小组成员
     * @param page 分页参数
     * @return 成员列表
     */
    @DataControl
    @RequestMapping(value = "group/list")
    public ModelAndView groupList(@ControlParam(table = "sign_project", column = "create_by", getMethodName = "getProjectId") GroupMember groupMember, Page page){
        page.setLimit(20);
        ModelAndView modelAndView = new ModelAndView("sign/signApplyList");
        List<GroupMember> list = signApplyService.groupMemberList(groupMember, page);
        SignGroup myGroup = signGroupService.get(groupMember.getGroupId());
        modelAndView.addObject("myGroup", myGroup);
        modelAndView.addObject("list", list);
        modelAndView.addObject("page", page);
        if (StringUtils.isNotEmpty(myGroup.getProjectId())) {
            SignProject signProject = signProjectService.get(myGroup.getProjectId());
            modelAndView.addObject("signProject", signProject);
        }
        return modelAndView;
    }

    /**
     * 分配小组
     * @param id 报名编号
     * @return 交互数据
     */
    @RequestMapping(value = "distribute")
    public ModelAndView distributeGroup(String id){
        ModelAndView modelAndView = new ModelAndView("sign/distributeGroup");
        SignApply signApply = signApplyService.get(id);

        SignGroup signGroup = new SignGroup();
        signGroup.setProjectId(signApply.getProjectId());
        List<SignGroup> signGroupList = signGroupService.list(signGroup);
        SignGroup s = new SignGroup();
        s.setName("无队伍");
        s.setId("");
        signGroupList.add(s);

        modelAndView.addObject("signGroupList", signGroupList);
        modelAndView.addObject("signApply", signApply);
        return modelAndView;
    }

    /**
     * 保存分配信息
     * @param id 报名编号
     * @param groupId 分组编号
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "distribute/save")
    public AjaxResult distributeSave(String id, String groupId) {
        try {
            AjaxResult ajaxResult = new AjaxResult();
            signApplyBizService.changeGroup(id, groupId);
            ajaxResult.setSuccess(true);
            return ajaxResult;
        } catch (Exception e) {
            logger.error("分配小组异常", e);
            return AjaxResult.error("分配小组异常");
        }
    }

    /**
     * 删除签到报名
     *
     * @param id 编号
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "delete")
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public AjaxResult delete(String id) {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error("id不能为空");
        }
        try {
            signApplyBizService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
            logger.error("删除人员异常", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error("删除人员异常");
        }
    }

    /**
     * 审核通过
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "verifyPass")
    public AjaxResult verifyPass(String id) {
        try {
            AjaxResult ajaxResult = new AjaxResult();
            SignApply signApply = new SignApply();
            signApply.setId(id);
            signApply.setStatus(1);
            boolean result = signApplyService.update(signApply);
            ajaxResult.setData(result);
            return ajaxResult;
        } catch (Exception e) {
            logger.error("审核通过", e);
            return AjaxResult.error("审核通过");
        }
    }

    /**
     * 审核拒绝
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "verifyReject")
    public AjaxResult verifyReject(String id) {
        try {
            AjaxResult ajaxResult = new AjaxResult();
            SignApply signApply = new SignApply();
            signApply.setId(id);
            signApply.setStatus(2);
            boolean result = signApplyService.update(signApply);
            ajaxResult.setData(result);
            return ajaxResult;
        } catch (Exception e) {
            logger.error("审核拒绝", e);
            return AjaxResult.error("审核拒绝");
        }
    }

    /**
     * 签到报名审核
     * @param status 状态
     * @param id 报名编号
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "check")
    public AjaxResult check(Integer status, String id) {
        try {
            AjaxResult ajaxResult = new AjaxResult();
            signApplyBizService.check(status, id);
            ajaxResult.setSuccess(true);
            return ajaxResult;
        } catch (Exception e) {
            logger.error("报名审核异常", e);
            return AjaxResult.error("报名审核异常");
        }
    }

    /**
     * 人员排行
     *
     * @param groupMember 查询条件
     * @param type        日期类型
     * @param page        分页参数
     * @return
     */
    @DataControl
    @RequestMapping("rankList")
    public ModelAndView rankList(@ControlParam(table = "sign_project", column = "create_by", getMethodName = "getProjectId")GroupMember groupMember, Integer type, Page page) {
        page.setLimit(20);
        ModelAndView mv = new ModelAndView("sign/signMemberRank");
        List<GroupMember> groupMembers = signApplyBizService.rankList(groupMember, type, page);
        mv.addObject("groupMembers", groupMembers);
        SignProject signProject = signProjectService.get(groupMember.getProjectId());
        mv.addObject("signProject", signProject);
        mv.addObject("type", type);
        mv.addObject("page", page);
        SignGroup signGroup = new SignGroup();
        signGroup.setProjectId(groupMember.getProjectId());
        List<SignGroup> signGroups = signGroupService.list(signGroup);
        mv.addObject("signGroups", signGroups);
        mv.addObject("page", page);
        return mv;
    }

    /**
     * 小组人员排行
     *
     * @param groupMember 查询条件
     * @param page        分页参数
     * @return
     */
    @DataControl
    @RequestMapping("group/rankList")
    public ModelAndView groupRankList(@ControlParam(table = "sign_project", column = "create_by", getMethodName = "getProjectId") GroupMember groupMember, Integer type, Page page) {
        if (StringUtils.isEmpty(groupMember.getGroupId())) {
            return new ModelAndView(Constant.ERROR_402);
        }
        SignGroup signGroup = signGroupService.get(groupMember.getGroupId());
        if (null == signGroup) {
            return new ModelAndView(Constant.ERROR_402);
        }
        page.setLimit(20);
        ModelAndView mv = new ModelAndView("sign/signGroupMemberRank");
        List<GroupMember> groupMembers = signApplyBizService.groupMemberList(groupMember, type, page);
        mv.addObject("groupMembers", groupMembers);

        mv.addObject("signGroup", signGroup);
        SignProject signProject = signProjectService.get(groupMember.getProjectId());
        mv.addObject("signProject", signProject);
        mv.addObject("type", type);
        mv.addObject("page", page);
        return mv;
    }

    /**
     * 我的打卡记录
     *
     * @param applyId 报名id
     * @param page    分页参数
     * @return
     */
    @RequestMapping("myRecordList")
    public ModelAndView myRecordList(String applyId, Page page) {
        page.setLimit(10);
        SignRecord signRecord = new SignRecord();
        signRecord.setApplyId(applyId);
        List<SignRecord> signRecords = signRecordService.listPage(signRecord, page);
        ModelAndView mv = new ModelAndView("sign/signMemberRecord");
        mv.addObject("records", signRecords);
        mv.addObject("applyId", applyId);
        mv.addObject("page", page);
        return mv;
    }
}
